﻿using HomagChina.CCHCS.Views;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;
using static HomagChina.CCHCS.ProcessResourceInternal;
using LocalText = Proxy.Properties.Resources;

namespace HomagChina.CCHCS.ViewModels
{
    public class JobListViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private CommonHandle commonHandle;
        private DeleteInfoHandle Handle_Delete;
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;

        public RelayCommand InfoCommand { get; }
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        #endregion Handle

        #region 属性

        private string _JobID;

        public string JobID
        {
            get => _JobID;
            set { _JobID = value; Query(); OnPropertyChanged(); }
        }

        private string _SelectionMode = "Multiple";

        public string SelectionMode
        {
            get => _SelectionMode;
            set
            {
                _SelectionMode = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<object> _SelectionList = new ObservableCollection<object>();

        public ObservableCollection<object> SelectionList
        {
            get => _SelectionList;
            set
            {
                _SelectionList = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<JobModel> _List_JobInfoModel = new ObservableCollection<JobModel>();

        public ObservableCollection<JobModel> List_JobInfoModel
        {
            get => _List_JobInfoModel;
            set { _List_JobInfoModel = value; OnPropertyChanged(); }
        }

        private DispatcherTimer UpdateSessionTimer;

        #endregion 属性

        #region 构造函数

        public JobListViewModel()
        {
            commonHandle = new CommonHandle();
            Handle_Delete = new DeleteInfoHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            InfoCommand = new RelayCommand(TaskInfo);
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);

            //Query();
            UpdateSessionTimer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(15000)
            };
            UpdateSessionTimer.Tick += UpdateSessionTimer_Tick;
        }

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewloadedCommand()
        {
            //UpdateSessionTimer.Start();
        }

        private void UpdateSessionTimer_Tick(object sender, EventArgs e)
        {
            Query();
        }

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewUnloadedCommand()
        {
            UpdateSessionTimer.Stop();
        }

        #endregion 构造函数

        #region 函数

        public void Query()
        {
            RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
            List_JobInfoModel = Handle_Search.GetJobList(JobID);

            foreach (var item in List_JobInfoModel)
            {
                item.JobType_Image = GetResourceBrush(item.JobType_Name);
                item.State_Image = GetResourceBrush(item.State_Name);
            }

            RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        ///手动进料
        /// </summary>
        /// <param name="obj"></param>yn
        public void HandleOpenManualInfeed()
        {
            ManualInfeedViewModel manualInfeedViewModel = new ManualInfeedViewModel();
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<ManualInfeed>(manualInfeedViewModel, null);
            //Query();
        }

        /// <summary>
        /// 手动出料
        /// </summary>
        public void HandleOpenManualOutfeed()
        {
            ManualOutfeedViewModel manualOutfeedViewModel = new ManualOutfeedViewModel();
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<ManualOutfeed>(manualOutfeedViewModel, null);
            //Query();
        }

        /// <summary>
        /// 导入脚本
        /// </summary>
        public void HandleOpenJobImport()
        {
            JobImportViewModel jobImportViewModel = new JobImportViewModel();
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<JobImport>(jobImportViewModel, null);
            //Query();
        }

        /// <summary>
        ///
        /// </summary>mar
        /// <param name="obj"></param>
        public void TaskInfo(object SelectItem)
        {
            var model = SelectItem as JobModel;
            TaskInfoViewModel taskInfoViewModel = new TaskInfoViewModel(model);
            TaskInfo taskInfo = new HomagChina.CCHCS.Views.TaskInfo();
            taskInfoViewModel.RequestScrollPositionRestore += taskInfo.ViewModel_RequestScrollPositionRestore;
            taskInfoViewModel.RequestScrollPositionSave += taskInfo.ViewModel_RequestScrollPositionSave;
            taskInfo.DataContext = taskInfoViewModel;
            UIAdjustments.CurrentThreadDialogManager.ShowDialog(taskInfo);

            //Query();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        public void HandleJobDelete()
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show(LocalText.Delete_jobs, LocalText.Prompt_message, MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                List<int> list = new List<int>();
                foreach (var item in SelectionList)
                {
                    list.Add((item as JobModel).Id);
                }
                Handle_Delete.DeletJobInfos(list);
                //Query();
            }
        }

        /// <summary>
        ///  取消任务
        /// </summary>
        /// <param name="obj"></param>
        public void HandleJobCancel()
        {
            if (SelectionList.Count != 0 && HomagGroup.Base.UI.Windows.MessageBox.Show(LocalText.Cancel_jobs, LocalText.Prompt_message, MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                List<int> list = new List<int>();
                foreach (var item in SelectionList)
                {
                    list.Add((item as JobModel).Id);
                }
                Handle_Delete.CancelJob(list);
                //Query();
            }
        }

        /// <summary>
        /// 置顶任务
        /// </summary>
        public void HandleMoveTop()
        {
            if (SelectionList.Count != 0)
            {
                List<int> list = new List<int>();
                foreach (var item in SelectionList)
                {
                    list.Add((item as JobModel).Id);
                }
                Handle_Modify.ModifyJobListMoveTop(list);
                //Query();
            }
        }

        /// <summary>
        ///
        /// </summary>
        public void DragExecute(ObservableCollection<JobModel> NewList_JobInfoModel)
        {
            Handle_Modify.ModifyJobListInfo(NewList_JobInfoModel);
            SelectionMode = "Multiple";
        }
    }

    #endregion 函数
}